#
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Bluefied Software Distribution Version
BFSOC_VERSION = 4.11.0
BFSOC_REVISION = 13582
BFB_IMG_TYPE = prod
BFSOC_BUILD_DATE =

BSD_INTERNAL_BASE_URL = 
BSD_BASE_URL = 
BSD_BASE_SOURCE_URL = 
BFSOC_FROM_INTERNAL = n

ifneq ($(BSD_INTERNAL_BASE_URL), )
BFSOC_FROM_INTERNAL = y
BSD_BASE_URL = $(BSD_INTERNAL_BASE_URL)
ifneq ($(BFSOC_BUILD_DATE), )
BSD_BASE_URL = $(BSD_INTERNAL_BASE_URL)/$(BFSOC_BUILD_DATE)
endif

BSD_BASE_SOURCE_URL = $(BSD_BASE_URL)/install/distro/SRPMS/
QP_BOOTIMAGES_BASE_URL = $(BSD_BASE_URL)/install/distro/DEBS/
DEV_BOOTIMAGES_BASE_URL = $(BSD_BASE_URL)/install/distro/dev-release/DEBS/
PROD_BOOTIMAGES_BASE_URL = $(BSD_BASE_URL)/install/distro/ga-release/DEBS/
endif

export BFSOC_VERSION BFSOC_REVISION BSD_BASE_URL BSD_BASE_SOURCE_URL BFB_IMG_TYPE BFSOC_FROM_INTERNAL PLATFORM_PATH

define make_url_bfsoc
	$(1)_URL="https://github.com/Mellanox/sonic-bluefield-packages/releases/download/dpu-bfsoc-$(BFSOC_VERSION)-$(BFSOC_REVISION)-$(BLDENV)/$(1)"

endef

BF_PLATFORM_DRIVERS =
BFSOC_MAKE_DEBS =
BFSOC_MAKE_DEBS_DERIVED =
BFSOC_ONLINE_DEBS =

MLXBF3_PINCTRL_DRIVER_VERSION = 1.0
MLXBF3_PINCTRL_DRIVER = pinctrl-mlxbf3.ko
$(MLXBF3_PINCTRL_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/pinctrl-mlxbf3
$(MLXBF3_PINCTRL_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLXBF3_PINCTRL_DRIVER)
export MLXBF3_PINCTRL_DRIVER_VERSION MLXBF3_PINCTRL_DRIVER

TMFIFO_DRIVER_VERSION = 1.7
TMFIFO_DRIVER = mlxbf-tmfifo.ko
$(TMFIFO_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/tmfifo
$(TMFIFO_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(TMFIFO_DRIVER)
export TMFIFO_DRIVER_VERSION TMFIFO_DRIVER

MLXBF_GIGE_DRIVER_VERSION = 1.1
MLXBF_GIGE_DRIVER = mlxbf-gige.ko
$(MLXBF_GIGE_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/mlxbf-gige
$(MLXBF_GIGE_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLXBF_GIGE_DRIVER)
export MLXBF_GIGE_DRIVER_VERSION MLXBF_GIGE_DRIVER

MLXBF3_GPIO_DRIVER_VERSION = 1.0
MLXBF3_GPIO_DRIVER = gpio-mlxbf3.ko
$(MLXBF3_GPIO_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/gpio-mlxbf3
$(MLXBF3_GPIO_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLXBF3_GPIO_DRIVER)
export MLXBF3_GPIO_DRIVER_VERSION MLXBF3_GPIO_DRIVER

ifeq ($(BFSOC_VERSION), 4.2.0)
MLX_BOOTCTL_DRIVER_VERSION = 1.6
else
MLX_BOOTCTL_DRIVER_VERSION = 1.7
endif
MLX_BOOTCTL_DRIVER = mlx-bootctl.ko
$(MLX_BOOTCTL_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/mlx-bootctl
$(MLX_BOOTCTL_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLX_BOOTCTL_DRIVER)
export MLX_BOOTCTL_DRIVER_VERSION MLX_BOOTCTL_DRIVER

SDHCI_OF_DWCMSHC_DRIVER_VERSION = 1.0
SDHCI_OF_DWCMSHC_DRIVER = sdhci-of-dwcmshc.ko
$(SDHCI_OF_DWCMSHC_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/sdhci-of-dwcmshc
$(SDHCI_OF_DWCMSHC_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(SDHCI_OF_DWCMSHC_DRIVER)
export SDHCI_OF_DWCMSHC_DRIVER_VERSION SDHCI_OF_DWCMSHC_DRIVER

MLXBF_BOOTCTL_DEB_VERSION = 2.1
MLXBF_BOOTCTL_DEB = mlxbf-bootctl_${MLXBF_BOOTCTL_DEB_VERSION}_${CONFIGURED_ARCH}.deb
$(MLXBF_BOOTCTL_DEB)_SRC_PATH = $(PLATFORM_PATH)/mlxbf-bootctl

BFSOC_MAKE_DEBS += $(MLXBF_BOOTCTL_DEB)
export MLXBF_BOOTCTL_DEB_VERSION MLXBF_BOOTCTL_DEB

MLXBF_PKA_DRIVER_VERSION = 1.0
MLXBF_PKA_DRIVER = mlxbf-pka.ko
$(MLXBF_PKA_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/mlxbf-pka
$(MLXBF_PKA_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLXBF_PKA_DRIVER)
export MLXBF_PKA_DRIVER_VERSION MLXBF_PKA_DRIVER

PWR_MLXBF_DRIVER_VERSION = 1.0
PWR_MLXBF_DRIVER = pwr-mlxbf.ko
$(PWR_MLXBF_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/pwr-mlxbf
$(PWR_MLXBF_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(PWR_MLXBF_DRIVER)
export PWR_MLXBF_DRIVER_VERSION PWR_MLXBF_DRIVER

MLXBF_PTM_DRIVER_VERSION = 1.1
MLXBF_PTM_DRIVER = mlxbf-ptm.ko
$(MLXBF_PTM_DRIVER)_SRC_PATH = $(PLATFORM_PATH)/mlxbf-ptm
$(MLXBF_PTM_DRIVER)_DEPENDS += $(LINUX_HEADERS) $(LINUX_HEADERS_COMMON)

BF_PLATFORM_DRIVERS += $(MLXBF_PTM_DRIVER)
export MLXBF_PTM_DRIVER_VERSION MLXBF_PTM_DRIVER

ifeq ($(BFB_IMG_TYPE), qp)
BOOTIMAGES_BASE_URL = $(QP_BOOTIMAGES_BASE_URL)
BOOTIMAGES = mlxbf-bootimages_$(BFSOC_VERSION)-$(BFSOC_REVISION)_arm64.deb
else ifeq ($(BFB_IMG_TYPE), prod)
BOOTIMAGES_BASE_URL = $(PROD_BOOTIMAGES_BASE_URL)
BOOTIMAGES = mlxbf-bootimages-signed_$(BFSOC_VERSION)-$(BFSOC_REVISION)_arm64.deb
else
BOOTIMAGES_BASE_URL = $(DEV_BOOTIMAGES_BASE_URL)
BOOTIMAGES = mlxbf-bootimages-devsigned_$(BFSOC_VERSION)-$(BFSOC_REVISION)_arm64.deb
endif

$(BOOTIMAGES)_URL = $(BOOTIMAGES_BASE_URL)/$(BOOTIMAGES)

BFSOC_ONLINE_DEBS += $(BOOTIMAGES)
export BOOTIMAGES BOOTIMAGES_BASE_URL

MLNX_BLUEFIELD_BUILD_SCRIPTS =  bfscripts_$(BFSOC_VERSION)-$(BFSOC_REVISION)_all.deb
$(MLNX_BLUEFIELD_BUILD_SCRIPTS)_SRC_PATH = $(PLATFORM_PATH)/bfscripts
$(MLNX_BLUEFIELD_BUILD_SCRIPTS)_DEPENDS += $(MLXBF_BOOTCTL_DEB) $(BOOTIMAGES)
$(MLNX_BLUEFIELD_BUILD_SCRIPTS)_RDEPENDS += $(MLXBF_BOOTCTL_DEB) $(BOOTIMAGES)

BFSOC_MAKE_DEBS += $(MLNX_BLUEFIELD_BUILD_SCRIPTS)
export MLNX_BLUEFIELD_BUILD_SCRIPTS

BF_PLATFORM_MODULE_VERSION = 1.0
BF_PLATFORM_MODULE = bluefield-platform-modules_$(BF_PLATFORM_MODULE_VERSION)_arm64.deb
$(BF_PLATFORM_MODULE)_SRC_PATH = $(PLATFORM_PATH)/bluefield-platform-modules

$(BF_PLATFORM_MODULE)_FILES = $(BF_PLATFORM_DRIVERS)

export BF_PLATFORM_MODULE_VERSION BF_PLATFORM_MODULE BF_PLATFORM_DRIVERS

ifeq ($(BFSOC_FROM_INTERNAL), y)
SONIC_MAKE_FILES += $(BF_PLATFORM_DRIVERS)
SONIC_MAKE_DEBS += $(BFSOC_MAKE_DEBS) $(BF_PLATFORM_MODULE)
SONIC_ONLINE_DEBS += $(BFSOC_ONLINE_DEBS)
else
$(eval $(foreach file, $(BFSOC_MAKE_DEBS) $(BFSOC_MAKE_DEBS_DERIVED) $(BFSOC_ONLINE_DEBS) $(BF_PLATFORM_MODULE), $(call make_url_bfsoc,$(file))))
SONIC_ONLINE_DEBS += $(BFSOC_MAKE_DEBS) $(BFSOC_MAKE_DEBS_DERIVED) $(BFSOC_ONLINE_DEBS) $(BF_PLATFORM_MODULE)
endif
